home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Programming / perlman / man / perlxs.txt < prev    next >
Encoding:
Text File  |  1999-09-09  |  44.4 KB  |  1,248 lines

  1. NAME
  2.        perlxs - XS language reference manual
  3.  
  4. DESCRIPTION
  5.        Introduction
  6.  
  7.        XS is a language used to create an extension interface
  8.        between Perl and some C library which one wishes to use
  9.        with Perl.  The XS interface is combined with the library
  10.        to create a new library which can be linked to Perl.  An
  11.        XSUB is a function in the XS language and is the core
  12.        component of the Perl application interface.
  13.  
  14.        The XS compiler is called xsubpp.  This compiler will
  15.        embed the constructs necessary to let an XSUB, which is
  16.        really a C function in disguise, manipulate Perl values
  17.        and creates the glue necessary to let Perl access the
  18.        XSUB.  The compiler uses typemaps to determine how to map
  19.        C function parameters and variables to Perl values.  The
  20.        default typemap handles many common C types.  A supplement
  21.        typemap must be created to handle special structures and
  22.        types for the library being linked.
  23.  
  24.        See the perlxstut manpage for a tutorial on the whole
  25.        extension creation process.
  26.  
  27.        On The Road
  28.  
  29.        Many of the examples which follow will concentrate on
  30.        creating an interface between Perl and the ONC+ RPC bind
  31.        library functions.  The rpcb_gettime() function is used to
  32.        demonstrate many features of the XS language.  This
  33.        function has two parameters; the first is an input
  34.        parameter and the second is an output parameter.  The
  35.        function also returns a status value.
  36.  
  37.                bool_t rpcb_gettime(const char *host, time_t *timep);
  38.  
  39.        From C this function will be called with the following
  40.        statements.
  41.  
  42.             #include <rpc/rpc.h>
  43.             bool_t status;
  44.             time_t timep;
  45.             status = rpcb_gettime( "localhost", &timep );
  46.  
  47.        If an XSUB is created to offer a direct translation
  48.        between this function and Perl, then this XSUB will be
  49.        used from Perl with the following code.  The $status and
  50.        $timep variables will contain the output of the function.
  51.  
  52.             use RPC;
  53.             $status = rpcb_gettime( "localhost", $timep );
  54.  
  55.        The following XS file shows an XS subroutine, or XSUB,
  56.        which demonstrates one possible interface to the
  57.        rpcb_gettime() function.  This XSUB represents a direct
  58.        translation between C and Perl and so preserves the
  59.        interface even from Perl.  This XSUB will be invoked from
  60.        Perl with the usage shown above.  Note that the first
  61.        three #include statements, for EXTERN.h, perl.h, and
  62.        XSUB.h, will always be present at the beginning of an XS
  63.        file.  This approach and others will be expanded later in
  64.        this document.
  65.  
  66.             #include "EXTERN.h"
  67.             #include "perl.h"
  68.             #include "XSUB.h"
  69.             #include <rpc/rpc.h>
  70.  
  71.             MODULE = RPC  PACKAGE = RPC
  72.  
  73.             bool_t
  74.             rpcb_gettime(host,timep)
  75.                  char *host
  76.                  time_t &timep
  77.                  OUTPUT:
  78.                  timep
  79.  
  80.        Any extension to Perl, including those containing XSUBs,
  81.        should have a Perl module to serve as the bootstrap which
  82.        pulls the extension into Perl.  This module will export
  83.        the extension's functions and variables to the Perl
  84.        program and will cause the extension's XSUBs to be linked
  85.        into Perl.  The following module will be used for most of
  86.        the examples in this document and should be used from Perl
  87.        with the use command as shown earlier.  Perl modules are
  88.        explained in more detail later in this document.
  89.  
  90.             package RPC;
  91.  
  92.             require Exporter;
  93.             require DynaLoader;
  94.             @ISA = qw(Exporter DynaLoader);
  95.             @EXPORT = qw( rpcb_gettime );
  96.  
  97.             bootstrap RPC;
  98.             1;
  99.  
  100.        Throughout this document a variety of interfaces to the
  101.        rpcb_gettime() XSUB will be explored.  The XSUBs will take
  102.        their parameters in different orders or will take
  103.        different numbers of parameters.  In each case the XSUB is
  104.        an abstraction between Perl and the real C rpcb_gettime()
  105.        function, and the XSUB must always ensure that the real
  106.        rpcb_gettime() function is called with the correct
  107.        parameters.  This abstraction will allow the programmer to
  108.        create a more Perl-like interface to the C function.
  109.        The Anatomy of an XSUB
  110.  
  111.        The following XSUB allows a Perl program to access a C
  112.        library function called sin().  The XSUB will imitate the
  113.        C function which takes a single argument and returns a
  114.        single value.
  115.  
  116.             double
  117.             sin(x)
  118.               double x
  119.  
  120.        When using C pointers the indirection operator * should be
  121.        considered part of the type and the address operator &
  122.        should be considered part of the variable, as is
  123.        demonstrated in the rpcb_gettime() function above.  See
  124.        the section on typemaps for more about handling qualifiers
  125.        and unary operators in C types.
  126.  
  127.        The function name and the return type must be placed on
  128.        separate lines.
  129.  
  130.          INCORRECT                        CORRECT
  131.  
  132.          double sin(x)                    double
  133.            double x                       sin(x)
  134.                                             double x
  135.  
  136.        The function body may be indented or left-adjusted.  The
  137.        following example shows a function with its body left-
  138.        adjusted.  Most examples in this document will indent the
  139.        body.
  140.  
  141.          CORRECT
  142.  
  143.          double
  144.          sin(x)
  145.          double x
  146.  
  147.        The Argument Stack
  148.  
  149.        The argument stack is used to store the values which are
  150.        sent as parameters to the XSUB and to store the XSUB's
  151.        return value.  In reality all Perl functions keep their
  152.        values on this stack at the same time, each limited to its
  153.        own range of positions on the stack.  In this document the
  154.        first position on that stack which belongs to the active
  155.        function will be referred to as position 0 for that
  156.        function.
  157.  
  158.        XSUBs refer to their stack arguments with the macro ST(x),
  159.        where x refers to a position in this XSUB's part of the
  160.        stack.  Position 0 for that function would be known to the
  161.        XSUB as ST(0).  The XSUB's incoming parameters and
  162.        outgoing return values always begin at ST(0).  For many
  163.        simple cases the xsubpp compiler will generate the code
  164.        necessary to handle the argument stack by embedding code
  165.        fragments found in the typemaps.  In more complex cases
  166.        the programmer must supply the code.
  167.  
  168.        The RETVAL Variable
  169.  
  170.        The RETVAL variable is a magic variable which always
  171.        matches the return type of the C library function.  The
  172.        xsubpp compiler will supply this variable in each XSUB and
  173.        by default will use it to hold the return value of the C
  174.        library function being called.  In simple cases the value
  175.        of RETVAL will be placed in ST(0) of the argument stack
  176.        where it can be received by Perl as the return value of
  177.        the XSUB.
  178.  
  179.        If the XSUB has a return type of void then the compiler
  180.        will not supply a RETVAL variable for that function.  When
  181.        using the PPCODE: directive the RETVAL variable may not be
  182.        needed.
  183.  
  184.        The MODULE Keyword
  185.  
  186.        The MODULE keyword is used to start the XS code and to
  187.        specify the package of the functions which are being
  188.        defined.  All text preceding the first MODULE keyword is
  189.        considered C code and is passed through to the output
  190.        untouched.  Every XS module will have a bootstrap function
  191.        which is used to hook the XSUBs into Perl.  The package
  192.        name of this bootstrap function will match the value of
  193.        the last MODULE statement in the XS source files.  The
  194.        value of MODULE should always remain constant within the
  195.        same XS file, though this is not required.
  196.  
  197.        The following example will start the XS code and will
  198.        place all functions in a package named RPC.
  199.  
  200.             MODULE = RPC
  201.  
  202.        The PACKAGE Keyword
  203.  
  204.        When functions within an XS source file must be separated
  205.        into packages the PACKAGE keyword should be used.  This
  206.        keyword is used with the MODULE keyword and must follow
  207.        immediately after it when used.
  208.  
  209.             MODULE = RPC  PACKAGE = RPC
  210.  
  211.             [ XS code in package RPC ]
  212.  
  213.             MODULE = RPC  PACKAGE = RPCB
  214.  
  215.             [ XS code in package RPCB ]
  216.  
  217.             MODULE = RPC  PACKAGE = RPC
  218.  
  219.             [ XS code in package RPC ]
  220.  
  221.        Although this keyword is optional and in some cases
  222.        provides redundant information it should always be used.
  223.        This keyword will ensure that the XSUBs appear in the
  224.        desired package.
  225.  
  226.        The PREFIX Keyword
  227.  
  228.        The PREFIX keyword designates prefixes which should be
  229.        removed from the Perl function names.  If the C function
  230.        is rpcb_gettime() and the PREFIX value is rpcb_ then Perl
  231.        will see this function as gettime().
  232.  
  233.        This keyword should follow the PACKAGE keyword when used.
  234.        If PACKAGE is not used then PREFIX should follow the
  235.        MODULE keyword.
  236.  
  237.             MODULE = RPC  PREFIX = rpc_
  238.  
  239.             MODULE = RPC  PACKAGE = RPCB  PREFIX = rpcb_
  240.  
  241.        The OUTPUT: Keyword
  242.  
  243.        The OUTPUT: keyword indicates that certain function
  244.        parameters should be updated (new values made visible to
  245.        Perl) when the XSUB terminates or that certain values
  246.        should be returned to the calling Perl function.  For
  247.        simple functions, such as the sin() function above, the
  248.        RETVAL variable is automatically designated as an output
  249.        value.  In more complex functions the xsubpp compiler will
  250.        need help to determine which variables are output
  251.        variables.
  252.  
  253.        This keyword will normally be used to complement the CODE:
  254.        keyword.  The RETVAL variable is not recognized as an
  255.        output variable when the CODE: keyword is present.  The
  256.        OUTPUT:  keyword is used in this situation to tell the
  257.        compiler that RETVAL really is an output variable.
  258.  
  259.        The OUTPUT: keyword can also be used to indicate that
  260.        function parameters are output variables.  This may be
  261.        necessary when a parameter has been modified within the
  262.        function and the programmer would like the update to be
  263.        seen by Perl.
  264.  
  265.  
  266.  
  267.             bool_t
  268.             rpcb_gettime(host,timep)
  269.                  char *host
  270.                  time_t &timep
  271.                  OUTPUT:
  272.                  timep
  273.  
  274.        The OUTPUT: keyword will also allow an output parameter to
  275.        be mapped to a matching piece of code rather than to a
  276.        typemap.
  277.  
  278.             bool_t
  279.             rpcb_gettime(host,timep)
  280.                  char *host
  281.                  time_t &timep
  282.                  OUTPUT:
  283.                  timep sv_setnv(ST(1), (double)timep);
  284.  
  285.        The CODE: Keyword
  286.  
  287.        This keyword is used in more complicated XSUBs which
  288.        require special handling for the C function.  The RETVAL
  289.        variable is available but will not be returned unless it
  290.        is specified under the OUTPUT: keyword.
  291.  
  292.        The following XSUB is for a C function which requires
  293.        special handling of its parameters.  The Perl usage is
  294.        given first.
  295.  
  296.             $status = rpcb_gettime( "localhost", $timep );
  297.  
  298.        The XSUB follows.
  299.  
  300.             bool_t
  301.             rpcb_gettime(host,timep)
  302.                  char *host
  303.                  time_t timep
  304.                  CODE:
  305.                       RETVAL = rpcb_gettime( host, &timep );
  306.                  OUTPUT:
  307.                  timep
  308.                  RETVAL
  309.  
  310.        The INIT: Keyword
  311.  
  312.        The INIT: keyword allows initialization to be inserted
  313.        into the XSUB before the compiler generates the call to
  314.        the C function.  Unlike the CODE: keyword above, this
  315.        keyword does not affect the way the compiler handles
  316.        RETVAL.
  317.  
  318.            bool_t
  319.            rpcb_gettime(host,timep)
  320.                  char *host
  321.                  time_t &timep
  322.                  INIT:
  323.                  printf("# Host is %s\n", host );
  324.                  OUTPUT:
  325.                  timep
  326.  
  327.        The NO_INIT Keyword
  328.  
  329.        The NO_INIT keyword is used to indicate that a function
  330.        parameter is being used as only an output value.  The
  331.        xsubpp compiler will normally generate code to read the
  332.        values of all function parameters from the argument stack
  333.        and assign them to C variables upon entry to the function.
  334.        NO_INIT will tell the compiler that some parameters will
  335.        be used for output rather than for input and that they
  336.        will be handled before the function terminates.
  337.  
  338.        The following example shows a variation of the
  339.        rpcb_gettime() function.  This function uses the timep
  340.        variable as only an output variable and does not care
  341.        about its initial contents.
  342.  
  343.             bool_t
  344.             rpcb_gettime(host,timep)
  345.                  char *host
  346.                  time_t &timep = NO_INIT
  347.                  OUTPUT:
  348.                  timep
  349.  
  350.        Initializing Function Parameters
  351.  
  352.        Function parameters are normally initialized with their
  353.        values from the argument stack.  The typemaps contain the
  354.        code segments which are used to transfer the Perl values
  355.        to the C parameters.  The programmer, however, is allowed
  356.        to override the typemaps and supply alternate
  357.        initialization code.
  358.  
  359.        The following code demonstrates how to supply
  360.        initialization code for function parameters.  The
  361.        initialization code is eval'd by the compiler before it is
  362.        added to the output so anything which should be
  363.        interpreted literally, such as double quotes, must be
  364.        protected with backslashes.
  365.  
  366.  
  367.  
  368.             bool_t
  369.             rpcb_gettime(host,timep)
  370.                  char *host = (char *)SvPV(ST(0),na);
  371.                  time_t &timep = 0;
  372.                  OUTPUT:
  373.                  timep
  374.  
  375.        This should not be used to supply default values for
  376.        parameters.  One would normally use this when a function
  377.        parameter must be processed by another library function
  378.        before it can be used.  Default parameters are covered in
  379.        the next section.
  380.  
  381.        Default Parameter Values
  382.  
  383.        Default values can be specified for function parameters by
  384.        placing an assignment statement in the parameter list.
  385.        The default value may be a number or a string.  Defaults
  386.        should always be used on the right-most parameters only.
  387.  
  388.        To allow the XSUB for rpcb_gettime() to have a default
  389.        host value the parameters to the XSUB could be rearranged.
  390.        The XSUB will then call the real rpcb_gettime() function
  391.        with the parameters in the correct order.  Perl will call
  392.        this XSUB with either of the following statements.
  393.  
  394.             $status = rpcb_gettime( $timep, $host );
  395.  
  396.             $status = rpcb_gettime( $timep );
  397.  
  398.        The XSUB will look like the code  which  follows.   A
  399.        CODE: block  is used to call the real rpcb_gettime()
  400.        function with the parameters in the correct order for that
  401.        function.
  402.  
  403.             bool_t
  404.             rpcb_gettime(timep,host="localhost")
  405.                  char *host
  406.                  time_t timep = NO_INIT
  407.                  CODE:
  408.                       RETVAL = rpcb_gettime( host, &timep );
  409.                  OUTPUT:
  410.                  timep
  411.                  RETVAL
  412.  
  413.        The PREINIT: Keyword
  414.  
  415.        The PREINIT: keyword allows extra variables to be declared
  416.        before the typemaps are expanded.  If a variable is
  417.        declared in a CODE: block then that variable will follow
  418.        any typemap code.  This may result in a C syntax error.
  419.        To force the variable to be declared before the typemap
  420.        code, place it into a PREINIT: block.  The PREINIT:
  421.        keyword may be used one or more times within an XSUB.
  422.  
  423.        The following examples are equivalent, but if the code is
  424.        using complex typemaps then the first example is safer.
  425.  
  426.             bool_t
  427.             rpcb_gettime(timep)
  428.                  time_t timep = NO_INIT
  429.                  PREINIT:
  430.                  char *host = "localhost";
  431.                  CODE:
  432.                  RETVAL = rpcb_gettime( host, &timep );
  433.                  OUTPUT:
  434.                  timep
  435.                  RETVAL
  436.  
  437.        A correct, but error-prone example.
  438.  
  439.             bool_t
  440.             rpcb_gettime(timep)
  441.                  time_t timep = NO_INIT
  442.                  CODE:
  443.                  char *host = "localhost";
  444.                  RETVAL = rpcb_gettime( host, &timep );
  445.                  OUTPUT:
  446.                  timep
  447.                  RETVAL
  448.  
  449.        The INPUT: Keyword
  450.  
  451.        The XSUB's parameters are usually evaluated immediately
  452.        after entering the XSUB.  The INPUT: keyword can be used
  453.        to force those parameters to be evaluated a little later.
  454.        The INPUT: keyword can be used multiple times within an
  455.        XSUB and can be used to list one or more input variables.
  456.        This keyword is used with the PREINIT: keyword.
  457.  
  458.        The following example shows how the input parameter timep
  459.        can be evaluated late, after a PREINIT.
  460.  
  461.            bool_t
  462.            rpcb_gettime(host,timep)
  463.                  char *host
  464.                  PREINIT:
  465.                  time_t tt;
  466.                  INPUT:
  467.                  time_t timep
  468.                  CODE:
  469.                       RETVAL = rpcb_gettime( host, &tt );
  470.                       timep = tt;
  471.                  OUTPUT:
  472.                  timep
  473.                  RETVAL
  474.        The next example shows each input parameter evaluated
  475.        late.
  476.  
  477.            bool_t
  478.            rpcb_gettime(host,timep)
  479.                  PREINIT:
  480.                  time_t tt;
  481.                  INPUT:
  482.                  char *host
  483.                  PREINIT:
  484.                  char *h;
  485.                  INPUT:
  486.                  time_t timep
  487.                  CODE:
  488.                       h = host;
  489.                       RETVAL = rpcb_gettime( h, &tt );
  490.                       timep = tt;
  491.                  OUTPUT:
  492.                  timep
  493.                  RETVAL
  494.  
  495.        Variable-length Parameter Lists
  496.  
  497.        XSUBs can have variable-length parameter lists by
  498.        specifying an ellipsis (...) in the parameter list.  This
  499.        use of the ellipsis is similar to that found in ANSI C.
  500.        The programmer is able to determine the number of
  501.        arguments passed to the XSUB by examining the items
  502.        variable which the xsubpp compiler supplies for all XSUBs.
  503.        By using this mechanism one can create an XSUB which
  504.        accepts a list of parameters of unknown length.
  505.  
  506.        The host parameter for the rpcb_gettime() XSUB can be
  507.        optional so the ellipsis can be used to indicate that the
  508.        XSUB will take a variable number of parameters.  Perl
  509.        should be able to call this XSUB with either of the
  510.        following statements.
  511.  
  512.             $status = rpcb_gettime( $timep, $host );
  513.  
  514.             $status = rpcb_gettime( $timep );
  515.  
  516.        The XS code, with ellipsis, follows.
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.             bool_t
  525.             rpcb_gettime(timep, ...)
  526.                  time_t timep = NO_INIT
  527.                  PREINIT:
  528.                  char *host = "localhost";
  529.                  CODE:
  530.                          if( items > 1 )
  531.                               host = (char *)SvPV(ST(1), na);
  532.                          RETVAL = rpcb_gettime( host, &timep );
  533.                  OUTPUT:
  534.                  timep
  535.                  RETVAL
  536.  
  537.        The PPCODE: Keyword
  538.  
  539.        The PPCODE: keyword is an alternate form of the CODE:
  540.        keyword and is used to tell the xsubpp compiler that the
  541.        programmer is supplying the code to control the argument
  542.        stack for the XSUBs return values.  Occasionally one will
  543.        want an XSUB to return a list of values rather than a
  544.        single value.  In these cases one must use PPCODE: and
  545.        then explicitly push the list of values on the stack.  The
  546.        PPCODE: and CODE:  keywords are not used together within
  547.        the same XSUB.
  548.  
  549.        The following XSUB will call the C rpcb_gettime() function
  550.        and will return its two output values, timep and status,
  551.        to Perl as a single list.
  552.  
  553.             void
  554.             rpcb_gettime(host)
  555.                  char *host
  556.                  PREINIT:
  557.                  time_t  timep;
  558.                  bool_t  status;
  559.                  PPCODE:
  560.                  status = rpcb_gettime( host, &timep );
  561.                  EXTEND(sp, 2);
  562.                  PUSHs(sv_2mortal(newSViv(status)));
  563.                  PUSHs(sv_2mortal(newSViv(timep)));
  564.  
  565.        Notice that the programmer must supply the C code
  566.        necessary to have the real rpcb_gettime() function called
  567.        and to have the return values properly placed on the
  568.        argument stack.
  569.  
  570.        The void return type for this function tells the xsubpp
  571.        compiler that the RETVAL variable is not needed or used
  572.        and that it should not be created.  In most scenarios the
  573.        void return type should be used with the PPCODE:
  574.        directive.
  575.  
  576.        The EXTEND() macro is used to make room on the argument
  577.        stack for 2 return values.  The PPCODE: directive causes
  578.        the xsubpp compiler to create a stack pointer called sp,
  579.        and it is this pointer which is being used in the EXTEND()
  580.        macro.  The values are then pushed onto the stack with the
  581.        PUSHs() macro.
  582.  
  583.        Now the rpcb_gettime() function can be used from Perl with
  584.        the following statement.
  585.  
  586.             ($status, $timep) = rpcb_gettime("localhost");
  587.  
  588.        Returning Undef And Empty Lists
  589.  
  590.        Occasionally the programmer will want to simply return
  591.        undef or an empty list if a function fails rather than a
  592.        separate status value.  The rpcb_gettime() function offers
  593.        just this situation.  If the function succeeds we would
  594.        like to have it return the time and if it fails we would
  595.        like to have undef returned.  In the following Perl code
  596.        the value of $timep will either be undef or it will be a
  597.        valid time.
  598.  
  599.             $timep = rpcb_gettime( "localhost" );
  600.  
  601.        The following XSUB uses the void return type to disable
  602.        the generation of the RETVAL variable and uses a CODE:
  603.        block to indicate to the compiler that the programmer has
  604.        supplied all the necessary code.  The sv_newmortal() call
  605.        will initialize the return value to undef, making that the
  606.        default return value.
  607.  
  608.             void
  609.             rpcb_gettime(host)
  610.                  char *  host
  611.                  PREINIT:
  612.                  time_t  timep;
  613.                  bool_t x;
  614.                  CODE:
  615.                  ST(0) = sv_newmortal();
  616.                  if( rpcb_gettime( host, &timep ) )
  617.                       sv_setnv( ST(0), (double)timep);
  618.  
  619.        The next example demonstrates how one would place an
  620.        explicit undef in the return value, should the need arise.
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.             void
  628.             rpcb_gettime(host)
  629.                  char *  host
  630.                  PREINIT:
  631.                  time_t  timep;
  632.                  bool_t x;
  633.                  CODE:
  634.                  ST(0) = sv_newmortal();
  635.                  if( rpcb_gettime( host, &timep ) ){
  636.                       sv_setnv( ST(0), (double)timep);
  637.                  }
  638.                  else{
  639.                       ST(0) = &sv_undef;
  640.                  }
  641.  
  642.        To return an empty list one must use a PPCODE: block and
  643.        then not push return values on the stack.
  644.  
  645.             void
  646.             rpcb_gettime(host)
  647.                  char *host
  648.                  PREINIT:
  649.                  time_t  timep;
  650.                  PPCODE:
  651.                  if( rpcb_gettime( host, &timep ) )
  652.                       PUSHs(sv_2mortal(newSViv(timep)));
  653.                  else{
  654.                  /* Nothing pushed on stack, so an empty */
  655.                  /* list is implicitly returned. */
  656.                  }
  657.  
  658.        Some people may be inclined to include an explicit return
  659.        in the above XSUB, rather than letting control fall
  660.        through to the end.  In those situations XSRETURN_EMPTY
  661.        should be used, instead.  This will ensure that the XSUB
  662.        stack is properly adjusted.  Consult the section on API
  663.        LISTING in the perlguts manpage for other XSRETURN macros.
  664.  
  665.        The REQUIRE: Keyword
  666.  
  667.        The REQUIRE: keyword is used to indicate the minimum
  668.        version of the xsubpp compiler needed to compile the XS
  669.        module.  An XS module which contains the following
  670.        statement will only compile with xsubpp version 1.922 or
  671.        greater:
  672.  
  673.                REQUIRE: 1.922
  674.  
  675.        The CLEANUP: Keyword
  676.  
  677.        This keyword can be used when an XSUB requires special
  678.        cleanup procedures before it terminates.  When the
  679.        CLEANUP:  keyword is used it must follow any CODE:,
  680.        PPCODE:, or OUTPUT: blocks which are present in the XSUB.
  681.        The code specified for the cleanup block will be added as
  682.        the last statements in the XSUB.
  683.  
  684.        The BOOT: Keyword
  685.  
  686.        The BOOT: keyword is used to add code to the extension's
  687.        bootstrap function.  The bootstrap function is generated
  688.        by the xsubpp compiler and normally holds the statements
  689.        necessary to register any XSUBs with Perl.  With the BOOT:
  690.        keyword the programmer can tell the compiler to add extra
  691.        statements to the bootstrap function.
  692.  
  693.        This keyword may be used any time after the first MODULE
  694.        keyword and should appear on a line by itself.  The first
  695.        blank line after the keyword will terminate the code
  696.        block.
  697.  
  698.             BOOT:
  699.             # The following message will be printed when the
  700.             # bootstrap function executes.
  701.             printf("Hello from the bootstrap!\n");
  702.  
  703.        The VERSIONCHECK: Keyword
  704.  
  705.        The VERSIONCHECK: keyword corresponds to xsubpp's
  706.        -versioncheck and -noversioncheck options.  This keyword
  707.        overrides the commandline options.  Version checking is
  708.        enabled by default.  When version checking is enabled the
  709.        XS module will attempt to verify that its version matches
  710.        the version of the PM module.
  711.  
  712.        To enable version checking:
  713.  
  714.            VERSIONCHECK: ENABLE
  715.  
  716.        To disable version checking:
  717.  
  718.            VERSIONCHECK: DISABLE
  719.  
  720.        The PROTOTYPES: Keyword
  721.  
  722.        The PROTOTYPES: keyword corresponds to xsubpp's
  723.        -prototypes and -noprototypes options.  This keyword
  724.        overrides the commandline options.  Prototypes are enabled
  725.        by default.  When prototypes are enabled XSUBs will be
  726.        given Perl prototypes.  This keyword may be used multiple
  727.        times in an XS module to enable and disable prototypes for
  728.        different parts of the module.
  729.  
  730.        To enable prototypes:
  731.  
  732.            PROTOTYPES: ENABLE
  733.  
  734.        To disable prototypes:
  735.  
  736.            PROTOTYPES: DISABLE
  737.  
  738.        The PROTOTYPE: Keyword
  739.  
  740.        This keyword is similar to the PROTOTYPES: keyword above
  741.        but can be used to force xsubpp to use a specific
  742.        prototype for the XSUB.  This keyword overrides all other
  743.        prototype options and keywords but affects only the
  744.        current XSUB.  Consult the Prototypes entry in the perlsub
  745.        manpage for information about Perl prototypes.
  746.  
  747.            bool_t
  748.            rpcb_gettime(timep, ...)
  749.                  time_t timep = NO_INIT
  750.                  PROTOTYPE: $;$
  751.                  PREINIT:
  752.                  char *host = "localhost";
  753.                  CODE:
  754.                          if( items > 1 )
  755.                               host = (char *)SvPV(ST(1), na);
  756.                          RETVAL = rpcb_gettime( host, &timep );
  757.                  OUTPUT:
  758.                  timep
  759.                  RETVAL
  760.  
  761.        The ALIAS: Keyword
  762.  
  763.        The ALIAS: keyword allows an XSUB to have two more more
  764.        unique Perl names and to know which of those names was
  765.        used when it was invoked.  The Perl names may be fully-
  766.        qualified with package names.  Each alias is given an
  767.        index.  The compiler will setup a variable called ix which
  768.        contain the index of the alias which was used.  When the
  769.        XSUB is called with its declared name ix will be 0.
  770.  
  771.        The following example will create aliases FOO::gettime()
  772.        and BAR::getit() for this function.
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.            bool_t
  781.            rpcb_gettime(host,timep)
  782.                  char *host
  783.                  time_t &timep
  784.                  ALIAS:
  785.                    FOO::gettime = 1
  786.                    BAR::getit = 2
  787.                  INIT:
  788.                  printf("# ix = %d\n", ix );
  789.                  OUTPUT:
  790.                  timep
  791.  
  792.        The INCLUDE: Keyword
  793.  
  794.        This keyword can be used to pull other files into the XS
  795.        module.  The other files may have XS code.  INCLUDE: can
  796.        also be used to run a command to generate the XS code to
  797.        be pulled into the module.
  798.  
  799.        The file Rpcb1.xsh contains our rpcb_gettime() function:
  800.  
  801.            bool_t
  802.            rpcb_gettime(host,timep)
  803.                  char *host
  804.                  time_t &timep
  805.                  OUTPUT:
  806.                  timep
  807.  
  808.        The XS module can use INCLUDE: to pull that file into it.
  809.  
  810.            INCLUDE: Rpcb1.xsh
  811.  
  812.        If the parameters to the INCLUDE: keyword are followed by
  813.        a pipe (|) then the compiler will interpret the parameters
  814.        as a command.
  815.  
  816.            INCLUDE: cat Rpcb1.xsh |
  817.  
  818.        The CASE: Keyword
  819.  
  820.        The CASE: keyword allows an XSUB to have multiple distinct
  821.        parts with each part acting as a virtual XSUB.  CASE: is
  822.        greedy and if it is used then all other XS keywords must
  823.        be contained within a CASE:.  This means nothing may
  824.        precede the first CASE: in the XSUB and anything following
  825.        the last CASE: is included in that case.
  826.  
  827.        A CASE: might switch via a parameter of the XSUB, via the
  828.        ix ALIAS: variable (see the section on The ALIAS:
  829.        Keyword), or maybe via the items variable (see the section
  830.        on Variable-length Parameter Lists).  The last CASE:
  831.        becomes the default case if it is not associated with a
  832.        conditional.  The following example shows CASE switched
  833.        via ix with a function rpcb_gettime() having an alias
  834.        x_gettime().  When the function is called as
  835.        rpcb_gettime() it's parameters are the usual (char *host,
  836.        time_t *timep), but when the function is called as
  837.        x_gettime() is parameters are reversed, (time_t *timep,
  838.        char *host).
  839.  
  840.            long
  841.            rpcb_gettime(a,b)
  842.              CASE: ix == 1
  843.                  ALIAS:
  844.                  x_gettime = 1
  845.                  INPUT:
  846.                  # 'a' is timep, 'b' is host
  847.                  char *b
  848.                  time_t a = NO_INIT
  849.                  CODE:
  850.                       RETVAL = rpcb_gettime( b, &a );
  851.                  OUTPUT:
  852.                  a
  853.                  RETVAL
  854.              CASE:
  855.                  # 'a' is host, 'b' is timep
  856.                  char *a
  857.                  time_t &b = NO_INIT
  858.                  OUTPUT:
  859.                  b
  860.                  RETVAL
  861.  
  862.        That function can be called with either of the following
  863.        statements.  Note the different argument lists.
  864.  
  865.                $status = rpcb_gettime( $host, $timep );
  866.  
  867.                $status = x_gettime( $timep, $host );
  868.  
  869.        The & Unary Operator
  870.  
  871.        The & unary operator is used to tell the compiler that it
  872.        should dereference the object when it calls the C
  873.        function.  This is used when a CODE: block is not used and
  874.        the object is a not a pointer type (the object is an int
  875.        or long but not a int* or long*).
  876.  
  877.        The following XSUB will generate incorrect C code.  The
  878.        xsubpp compiler will turn this into code which calls
  879.        rpcb_gettime() with parameters (char *host, time_t timep),
  880.        but the real rpcb_gettime() wants the timep parameter to
  881.        be of type time_t* rather than time_t.
  882.  
  883.  
  884.            bool_t
  885.            rpcb_gettime(host,timep)
  886.                  char *host
  887.                  time_t timep
  888.                  OUTPUT:
  889.                  timep
  890.  
  891.        That problem is corrected by using the & operator.  The
  892.        xsubpp compiler will now turn this into code which calls
  893.        rpcb_gettime() correctly with parameters (char *host,
  894.        time_t *timep).  It does this by carrying the & through,
  895.        so the function call looks like rpcb_gettime(host,
  896.        &timep).
  897.  
  898.            bool_t
  899.            rpcb_gettime(host,timep)
  900.                  char *host
  901.                  time_t &timep
  902.                  OUTPUT:
  903.                  timep
  904.  
  905.        Inserting Comments and C Preprocessor Directives
  906.  
  907.        C preprocessor directives are allowed within BOOT:,
  908.        PREINIT: INIT:, CODE:, PPCODE: and CLEANUP: blocks, as
  909.        well as outside the functions.  Comments are allowed
  910.        anywhere after the MODULE keyword.  The compiler will pass
  911.        the preprocessor directives through untouched and will
  912.        remove the commented lines.  Comments can be added to
  913.        XSUBs by placing a # as the first non-whitespace of a
  914.        line.  Care should be taken to avoid making the comment
  915.        look like a C preprocessor directive, lest it be
  916.        interpreted as such.  The simplest way to prevent this is
  917.        to put whitespace in front of the #.
  918.  
  919.        If you use preprocessor directives to choose one of two
  920.        versions of a function, use
  921.  
  922.            #if ... version1
  923.            #else /* ... version2  */
  924.            #endif
  925.  
  926.        and not
  927.  
  928.            #if ... version1
  929.            #endif
  930.            #if ... version2
  931.            #endif
  932.  
  933.        because otherwise xsubpp will believe that you made a
  934.        duplicate definition of the function.  Also, put a blank
  935.        line before the #else/#endif so it will not be seen as
  936.        part of the function body.
  937.        Using XS With C++
  938.  
  939.        If a function is defined as a C++ method then it will
  940.        assume its first argument is an object pointer.  The
  941.        object pointer will be stored in a variable called THIS.
  942.        The object should have been created by C++ with the new()
  943.        function and should be blessed by Perl with the
  944.        sv_setref_pv() macro.  The blessing of the object by Perl
  945.        can be handled by a typemap.  An example typemap is shown
  946.        at the end of this section.
  947.  
  948.        If the method is defined as static it will call the C++
  949.        function using the class::method() syntax.  If the method
  950.        is not static the function will be called using the
  951.        THIS->method() syntax.
  952.  
  953.        The next examples will use the following C++ class.
  954.  
  955.             class color {
  956.                  public:
  957.                  color();
  958.                  ~color();
  959.                  int blue();
  960.                  void set_blue( int );
  961.  
  962.                  private:
  963.                  int c_blue;
  964.             };
  965.  
  966.        The XSUBs for the blue() and set_blue() methods are
  967.        defined with the class name but the parameter for the
  968.        object (THIS, or "self") is implicit and is not listed.
  969.  
  970.             int
  971.             color::blue()
  972.  
  973.             void
  974.             color::set_blue( val )
  975.                  int val
  976.  
  977.        Both functions will expect an object as the first
  978.        parameter.  The xsubpp compiler will call that object THIS
  979.        and will use it to call the specified method.  So in the
  980.        C++ code the blue() and set_blue() methods will be called
  981.        in the following manner.
  982.  
  983.             RETVAL = THIS->blue();
  984.  
  985.             THIS->set_blue( val );
  986.  
  987.        If the function's name is DESTROY then the C++ delete
  988.        function will be called and THIS will be given as its
  989.        parameter.
  990.  
  991.             void
  992.             color::DESTROY()
  993.  
  994.        The C++ code will call delete.
  995.  
  996.             delete THIS;
  997.  
  998.        If the function's name is new then the C++ new function
  999.        will be called to create a dynamic C++ object.  The XSUB
  1000.        will expect the class name, which will be kept in a
  1001.        variable called CLASS, to be given as the first argument.
  1002.  
  1003.             color *
  1004.             color::new()
  1005.  
  1006.        The C++ code will call new.
  1007.  
  1008.                RETVAL = new color();
  1009.  
  1010.        The following is an example of a typemap that could be
  1011.        used for this C++ example.
  1012.  
  1013.            TYPEMAP
  1014.            color *             O_OBJECT
  1015.  
  1016.            OUTPUT
  1017.            # The Perl object is blessed into 'CLASS', which should be a
  1018.            # char* having the name of the package for the blessing.
  1019.            O_OBJECT
  1020.                sv_setref_pv( $arg, CLASS, (void*)$var );
  1021.  
  1022.            INPUT
  1023.            O_OBJECT
  1024.                if( sv_isobject($arg) && (SvTYPE(SvRV($arg)) == SVt_PVMG) )
  1025.                        $var = ($type)SvIV((SV*)SvRV( $arg ));
  1026.                else{
  1027.                        warn( \"${Package}::$func_name() -- $var is not a blessed SV reference\" );
  1028.                        XSRETURN_UNDEF;
  1029.                }
  1030.  
  1031.        Interface Strategy
  1032.  
  1033.        When designing an interface between Perl and a C library a
  1034.        straight translation from C to XS is often sufficient.
  1035.        The interface will often be very C-like and occasionally
  1036.        nonintuitive, especially when the C function modifies one
  1037.        of its parameters.  In cases where the programmer wishes
  1038.        to create a more Perl-like interface the following
  1039.        strategy may help to identify the more critical parts of
  1040.        the interface.
  1041.  
  1042.        Identify the C functions which modify their parameters.
  1043.        The XSUBs for these functions may be able to return lists
  1044.        to Perl, or may be candidates to return undef or an empty
  1045.        list in case of failure.
  1046.  
  1047.        Identify which values are used by only the C and XSUB
  1048.        functions themselves.  If Perl does not need to access the
  1049.        contents of the value then it may not be necessary to
  1050.        provide a translation for that value from C to Perl.
  1051.  
  1052.        Identify the pointers in the C function parameter lists
  1053.        and return values.  Some pointers can be handled in XS
  1054.        with the & unary operator on the variable name while
  1055.        others will require the use of the * operator on the type
  1056.        name.  In general it is easier to work with the &
  1057.        operator.
  1058.  
  1059.        Identify the structures used by the C functions.  In many
  1060.        cases it may be helpful to use the T_PTROBJ typemap for
  1061.        these structures so they can be manipulated by Perl as
  1062.        blessed objects.
  1063.  
  1064.        Perl Objects And C Structures
  1065.  
  1066.        When dealing with C structures one should select either
  1067.        T_PTROBJ or T_PTRREF for the XS type.  Both types are
  1068.        designed to handle pointers to complex objects.  The
  1069.        T_PTRREF type will allow the Perl object to be unblessed
  1070.        while the T_PTROBJ type requires that the object be
  1071.        blessed.  By using T_PTROBJ one can achieve a form of
  1072.        type-checking because the XSUB will attempt to verify that
  1073.        the Perl object is of the expected type.
  1074.  
  1075.        The following XS code shows the getnetconfigent() function
  1076.        which is used with ONC+ TIRPC.  The getnetconfigent()
  1077.        function will return a pointer to a C structure and has
  1078.        the C prototype shown below.  The example will demonstrate
  1079.        how the C pointer will become a Perl reference.  Perl will
  1080.        consider this reference to be a pointer to a blessed
  1081.        object and will attempt to call a destructor for the
  1082.        object.  A destructor will be provided in the XS source to
  1083.        free the memory used by getnetconfigent().  Destructors in
  1084.        XS can be created by specifying an XSUB function whose
  1085.        name ends with the word DESTROY.  XS destructors can be
  1086.        used to free memory which may have been malloc'd by
  1087.        another XSUB.
  1088.  
  1089.             struct netconfig *getnetconfigent(const char *netid);
  1090.  
  1091.        A typedef will be created for struct netconfig.  The Perl
  1092.        object will be blessed in a class matching the name of the
  1093.        C type, with the tag Ptr appended, and the name should not
  1094.        have embedded spaces if it will be a Perl package name.
  1095.        The destructor will be placed in a class corresponding to
  1096.        the class of the object and the PREFIX keyword will be
  1097.        used to trim the name to the word DESTROY as Perl will
  1098.        expect.
  1099.  
  1100.             typedef struct netconfig Netconfig;
  1101.  
  1102.             MODULE = RPC  PACKAGE = RPC
  1103.  
  1104.             Netconfig *
  1105.             getnetconfigent(netid)
  1106.                  char *netid
  1107.  
  1108.             MODULE = RPC  PACKAGE = NetconfigPtr  PREFIX = rpcb_
  1109.  
  1110.             void
  1111.             rpcb_DESTROY(netconf)
  1112.                  Netconfig *netconf
  1113.                  CODE:
  1114.                  printf("Now in NetconfigPtr::DESTROY\n");
  1115.                  free( netconf );
  1116.  
  1117.        This example requires the following typemap entry.
  1118.        Consult the typemap section for more information about
  1119.        adding new typemaps for an extension.
  1120.  
  1121.             TYPEMAP
  1122.             Netconfig *  T_PTROBJ
  1123.  
  1124.        This example will be used with the following Perl
  1125.        statements.
  1126.  
  1127.             use RPC;
  1128.             $netconf = getnetconfigent("udp");
  1129.  
  1130.        When Perl destroys the object referenced by $netconf it
  1131.        will send the object to the supplied XSUB DESTROY
  1132.        function.  Perl cannot determine, and does not care, that
  1133.        this object is a C struct and not a Perl object.  In this
  1134.        sense, there is no difference between the object created
  1135.        by the getnetconfigent() XSUB and an object created by a
  1136.        normal Perl subroutine.
  1137.  
  1138.        The Typemap
  1139.  
  1140.        The typemap is a collection of code fragments which are
  1141.        used by the xsubpp compiler to map C function parameters
  1142.        and values to Perl values.  The typemap file may consist
  1143.        of three sections labeled TYPEMAP, INPUT, and OUTPUT.  The
  1144.        INPUT section tells the compiler how to translate Perl
  1145.        values into variables of certain C types.  The OUTPUT
  1146.        section tells the compiler how to translate the values
  1147.        from certain C types into values Perl can understand.  The
  1148.        TYPEMAP section tells the compiler which of the INPUT and
  1149.        OUTPUT code fragments should be used to map a given C type
  1150.        to a Perl value.  Each of the sections of the typemap must
  1151.        be preceded by one of the TYPEMAP, INPUT, or OUTPUT
  1152.        keywords.
  1153.  
  1154.        The default typemap in the ext directory of the Perl
  1155.        source contains many useful types which can be used by
  1156.        Perl extensions.  Some extensions define additional
  1157.        typemaps which they keep in their own directory.  These
  1158.        additional typemaps may reference INPUT and OUTPUT maps in
  1159.        the main typemap.  The xsubpp compiler will allow the
  1160.        extension's own typemap to override any mappings which are
  1161.        in the default typemap.
  1162.  
  1163.        Most extensions which require a custom typemap will need
  1164.        only the TYPEMAP section of the typemap file.  The custom
  1165.        typemap used in the getnetconfigent() example shown
  1166.        earlier demonstrates what may be the typical use of
  1167.        extension typemaps.  That typemap is used to equate a C
  1168.        structure with the T_PTROBJ typemap.  The typemap used by
  1169.        getnetconfigent() is shown here.  Note that the C type is
  1170.        separated from the XS type with a tab and that the C unary
  1171.        operator * is considered to be a part of the C type name.
  1172.  
  1173.             TYPEMAP
  1174.             Netconfig *<tab>T_PTROBJ
  1175.  
  1176. EXAMPLES
  1177.        File RPC.xs: Interface to some ONC+ RPC bind library
  1178.        functions.
  1179.  
  1180.             #include "EXTERN.h"
  1181.             #include "perl.h"
  1182.             #include "XSUB.h"
  1183.  
  1184.             #include <rpc/rpc.h>
  1185.  
  1186.             typedef struct netconfig Netconfig;
  1187.  
  1188.             MODULE = RPC  PACKAGE = RPC
  1189.  
  1190.             void
  1191.             rpcb_gettime(host="localhost")
  1192.                  char *host
  1193.                  PREINIT:
  1194.                  time_t  timep;
  1195.                  CODE:
  1196.                  ST(0) = sv_newmortal();
  1197.                  if( rpcb_gettime( host, &timep ) )
  1198.                       sv_setnv( ST(0), (double)timep );
  1199.  
  1200.             Netconfig *
  1201.             getnetconfigent(netid="udp")
  1202.                  char *netid
  1203.  
  1204.             MODULE = RPC  PACKAGE = NetconfigPtr  PREFIX = rpcb_
  1205.             void
  1206.             rpcb_DESTROY(netconf)
  1207.                  Netconfig *netconf
  1208.                  CODE:
  1209.                  printf("NetconfigPtr::DESTROY\n");
  1210.                  free( netconf );
  1211.  
  1212.        File typemap: Custom typemap for RPC.xs.
  1213.  
  1214.             TYPEMAP
  1215.             Netconfig *  T_PTROBJ
  1216.  
  1217.        File RPC.pm: Perl module for the RPC extension.
  1218.  
  1219.             package RPC;
  1220.  
  1221.             require Exporter;
  1222.             require DynaLoader;
  1223.             @ISA = qw(Exporter DynaLoader);
  1224.             @EXPORT = qw(rpcb_gettime getnetconfigent);
  1225.  
  1226.             bootstrap RPC;
  1227.             1;
  1228.  
  1229.        File rpctest.pl: Perl test program for the RPC extension.
  1230.  
  1231.             use RPC;
  1232.  
  1233.             $netconf = getnetconfigent();
  1234.             $a = rpcb_gettime();
  1235.             print "time = $a\n";
  1236.             print "netconf = $netconf\n";
  1237.  
  1238.             $netconf = getnetconfigent("tcp");
  1239.             $a = rpcb_gettime("poplar");
  1240.             print "time = $a\n";
  1241.             print "netconf = $netconf\n";
  1242.  
  1243. XS VERSION
  1244.        This document covers features supported by xsubpp 1.935.
  1245.  
  1246. AUTHOR
  1247.        Dean Roehrich <roehrich@cray.com> Mar 12, 1996
  1248.